﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.QueryAndReport;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockOutDetailNew : Form
    {
        private StockOutHeader stockOutHeaderBLL;
        private StockOutDetail stockOutDetailBLL;
        private Drug drugBLL;

        public frmStockOutDetailNew()
        {
            InitializeComponent();

            stockOutHeaderBLL = new StockOutHeader();
            stockOutDetailBLL = new StockOutDetail();

        }

        /// <summary>
        /// 接收传回来的header，并设置根据其信息
        /// </summary>
        /// <param name="header"></param>
        public void Init(StockOutHeader header)
        {
            stockOutHeaderBLL = header;

            SetInfo();
        }

        /// <summary>
        ///  设置信息
        /// </summary>
        public void SetInfo()
        {
            txtStockOutNum.Text = stockOutHeaderBLL.StockOutNum;
            txtStockOutDatetime.Text = stockOutHeaderBLL.StockOutDatetime.ToString();
            txtStockOutType.Text = stockOutHeaderBLL.StockOutTypeName.StockOutTypeName;
            txtDepartment.Text = stockOutHeaderBLL.DepartmentName.DepartmentName;            
        }

        /// <summary>
        /// 计算金额
        /// </summary>
        public void Calcluate()
        {
            float price, amount;
            long qty;

            if (txtOutportPrice.Text.Trim() != "")
            {
                price = Convert.ToSingle(txtOutportPrice.Text.Trim());
            }
            else
            {
                price = 0;
            }
            if (txtQuantity.Text.Trim() != "")
            {
                qty = Convert.ToInt64(txtQuantity.Text.Trim());
            }
            else
            {
                qty = 0;
            }
            amount = price * qty;
            txtAmount.Text = Convert.ToString(amount);
        }

        /// <summary>
        /// 作“确定”按钮的可用状态的检查，填写好必要信息才能可用
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            if (txtDrugNum.Text.Trim() != "" && txtOutportPrice.Text.Trim() != "" && txtQuantity.Text.Trim() != "")
            {
                return btnOK.Enabled = true;
            }
            else
            {
                return btnOK.Enabled = false;
            }
        }

        /// <summary>
        /// 根据已填信息构建对象
        /// </summary>
        /// <returns></returns>
        public StockOutDetail GetObject()
        {
            StockOutDetail detail = new StockOutDetail();
            detail.DrugNum = new Drug();
            detail.Amount = Convert.ToSingle(txtAmount.Text.Trim());
            detail.DrugNum.DrugNum = Convert.ToInt32(txtDrugNum.Text.Trim());
            detail.OutportPrice = Convert.ToSingle(txtOutportPrice.Text.Trim());
            detail.RealQuantity = Convert.ToInt32(txtOutportPrice.Text.Trim());
            detail.StockOutNum = txtStockOutNum.Text.Trim();

            return detail;

        }

        /// <summary>
        /// 出库做好判断，超过库存量的不能出库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtQuantity.Text.Trim()) > Convert.ToInt32(txtStockQuantity.Text.Trim()))
            {
                MessageBox.Show("出库数量不能超过当前库存量！", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                stockOutDetailBLL.CreateStockOutDetail(GetObject());
                DialogResult = DialogResult.OK;
            }
        }

        /// <summary>
        /// 重置各种相关信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            txtDrugName.Text = txtDrugNum.Text = txtMeasuringUnit.Text = txtOutportPrice.Text = txtQuantity.Text = txtRetailPrice.Text = txtSpecifications.Text = txtTradePrice.Text = txtStockQuantity.Text = "";
           txtAmount.Text = "0";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 获取药品基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            frmDrugInfo form = new frmDrugInfo();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                txtDrugNum.Text = Convert.ToString(form.DrugBLL.DrugNum);
                txtDrugName.Text = form.DrugBLL.TradeName;
                txtSpecifications.Text = form.DrugBLL.Sepcifications;
                txtMeasuringUnit.Text = form.DrugBLL.MeasuringUnitNum.MeasuringUnitName;
                txtRetailPrice.Text = form.DrugBLL.RetailPrice.ToString();
                txtTradePrice.Text = form.DrugBLL.TradePrice.ToString();
                txtStockQuantity.Text = form.InventoryBLL.StockQuantity.ToString();
            }
            Error();
        }

        /// <summary>
        /// 设置其只能输入数字(可以有小数点)，并根据出库价和数量计算金额，做好按钮可用状态的检查
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtOutportPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtOutportPrice, '.');
            this.Calcluate();
            this.Check();
        }

        /// <summary>
        /// 设置其只能输入数字，并根据出库价和数量计算金额，做好按钮可用状态的检查
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtQuantity, '0');
            this.Check();
            this.Calcluate();
        }

        /// <summary>
        /// 检查库存是否充足
        /// </summary>
        public void Error()
        {
            if (Convert.ToInt32(txtStockQuantity.Text) <= 0)
            {
                MessageBox.Show("库存量不足，不能出库！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtQuantity.Enabled = txtOutportPrice.Enabled = false;

            }
        }
    }
}